/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

/*
 * VarEditScrean.java
 *
 * Created on 08.09.2012, 8:15:59
 */

package org.opu.odg.m_ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.AbstractButton;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import org.opu.odg.m_ui.beans.ContentBean;
import org.opu.odg.m_ui.beans.VariableBean;

/**
 *
 * @author yura
 */
public class VarEditScrean extends javax.swing.JDialog {

    private ResourceBundle bundle = ResourceBundle.getBundle("org.opu.odg.m_ui.Bundle");
    private VariableBean var;
    private VarChangesListener varChangesListener;
    private ContentBean previousContent;

    private ButtonGroup buttonGroup;

    public VarEditScrean(java.awt.Dialog parent, boolean modal, VariableBean var,
            VarChangesListener varChangesListener) {
        super(parent, modal);
        this.var = var;
        this.varChangesListener = varChangesListener;
        previousContent = var.getCurrentContent();
        initComponents();

        Box b = Box.createVerticalBox();
        int i = 10;
        buttonGroup = new ButtonGroup();
        for (ContentRadioButton contentCheckBox : createContentCheckBoxs(buttonGroup)) {
            Box hb = Box.createHorizontalBox();
            hb.add(Box.createHorizontalStrut(i));
            hb.add(contentCheckBox);
            hb.add(Box.createHorizontalGlue());
            b.add(hb);
            b.add(Box.createVerticalStrut(5));
            i += 20;
        }

        contentsPanel.add(b, BorderLayout.NORTH);
        setSize(400, 400);
        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bottomPanel = new javax.swing.JPanel();
        cancelBtn = new javax.swing.JButton();
        okBtn = new javax.swing.JButton();
        contentPane = new javax.swing.JPanel();
        contentsScroll = new javax.swing.JScrollPane();
        contentsPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        cancelBtn.setText(bundle.getString("cancel")); // NOI18N
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBtnActionPerformed(evt);
            }
        });
        bottomPanel.add(cancelBtn);

        okBtn.setText(bundle.getString("Save")); // NOI18N
        okBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okBtnActionPerformed(evt);
            }
        });
        bottomPanel.add(okBtn);

        getContentPane().add(bottomPanel, java.awt.BorderLayout.PAGE_END);

        contentPane.setLayout(new java.awt.BorderLayout());

        contentsScroll.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("template.edit.varcontent") + " \"" + var.getName() + "\""));

        contentsPanel.setBackground(java.awt.Color.white);
        contentsPanel.setLayout(new java.awt.BorderLayout());
        contentsScroll.setViewportView(contentsPanel);

        contentPane.add(contentsScroll, java.awt.BorderLayout.CENTER);

        getContentPane().add(contentPane, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
        dispose();
    }//GEN-LAST:event_cancelBtnActionPerformed

    private void okBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okBtnActionPerformed
        ButtonModel selection = buttonGroup.getSelection();
        ContentRadioButton selectedContent = null;
        for (Enumeration e = buttonGroup.getElements(); e.hasMoreElements();) {
            AbstractButton nextElement = (AbstractButton) e.nextElement();
            if (nextElement.getModel().equals(selection)) {
                selectedContent = (ContentRadioButton) nextElement;
            }
        }
        var.setCurrentContent(selectedContent.content);
        varChangesListener.applyVarChanges(previousContent, var);
        dispose();
    }//GEN-LAST:event_okBtnActionPerformed


    private List<ContentRadioButton> createContentCheckBoxs(ButtonGroup bg) {
        List<ContentRadioButton> list = new ArrayList<ContentRadioButton>();
        ContentBean currentContent = var.getDefaultContent();
        while (currentContent != null) {
            ContentRadioButton contentRadioButton = new ContentRadioButton(
                    currentContent.getName(), currentContent);
            list.add(contentRadioButton);
            bg.add(contentRadioButton);
            currentContent = currentContent.getParent();
        }
        ContentRadioButton contentRadioButton = new ContentRadioButton(
                bundle.getString("global_variables"), null);
        list.add(contentRadioButton);
        bg.add(contentRadioButton);

        Collections.reverse(list);

        if (previousContent != null) {
            for (ContentRadioButton contentCheckBox : list) {
                if (previousContent.equals(contentCheckBox.content)) {
                    bg.setSelected(contentCheckBox.getModel(), true);
                }
            }
        } else {
            bg.setSelected(contentRadioButton.getModel(), true);
        }

        return list;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bottomPanel;
    private javax.swing.JButton cancelBtn;
    private javax.swing.JPanel contentPane;
    private javax.swing.JPanel contentsPanel;
    private javax.swing.JScrollPane contentsScroll;
    private javax.swing.JButton okBtn;
    // End of variables declaration//GEN-END:variables

    static class ContentRadioButton extends JRadioButton {
        final ContentBean content;

        public ContentRadioButton(String text, ContentBean content) {
            super(text);
            this.content = content;
        }
    }

}
